#!/bin/sh

if [ $# -ne 2 ]
then
  echo "Usage: $0 CHAMPIONS CHALLENGERS"
  echo "   both arguments are files containing a list of monster names"
  exit 1
fi

CHAMPIONS=$1
CHALLENGERS=$2

TIMESTAMP=$(date "+%y%m%d%H")
[ -f Kerkerkruip.gblorb ] && rm Kerkerkruip.gblorb
if !  ./check-build.sh
then
  echo "Build failed."
  exit 1
fi
COMMIT=$(cat COMMIT)
OUTFILE=roundrobin.$TIMESTAMP-$COMMIT.results
[ -f "$OUTFILE" ] && rm "$OUTFILE"
echo "Pairing all $CHAMPIONS vs $CHALLENGERS, writing output to $OUTFILE..."
cat $CHAMPIONS | while read CHAMPION
do
  cat $CHALLENGERS | while read CHALLENGER
  do
    [ "$CHAMPION" = "$CHALLENGER" ] && break
    ./runner "$CHAMPION" "$CHALLENGER" | tail -n1 >>$OUTFILE
    tail -n1 $OUTFILE
  done
done
